﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Ajax : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Context.Response.ContentType = "text/plain";
        //String stockcode = Request["stockcode"];

        //String json = "[{\"stockname\":\""+stockcode+"\",\"xdata\":[\"2011-01-01\",\"2011-01-02\",\"2011-01-03\",\"2011-01-04\",\"2011-01-05\",\"2011-01-06\",\"2011-01-07\",\"2011-01-08\",\"2011-01-09\",\"2011-01-10\",\"2011-01-11\"],\"ydata\":[1,1.23,1.30,1.15,1.05,1.12,1.24,1.53,1.59,1.59,1.43]}]";
        String json = String.Empty;
        json = GetStockPriceInfo();
        Response.Write(json);
        Response.End();
    }

    private String GetStockPriceInfo()
    {
        String json = "\"stockname\":\"{0}\",\"xdata\":{1},\"ydata\":{2},\"isSuccess\":\"{3}\"";
        String xdata ="0";
        String ydata = "0";
        String isSuccess = "0";

        SQLExpressDAL.StockTableAdapters.StockUpdateTableAdapter adp = new SQLExpressDAL.StockTableAdapters.StockUpdateTableAdapter();
        SQLExpressDAL.Stock.StockUpdateDataTable table=null;

        table=adp.GetStockEveryDayRecord(Request["stockcode"]);
        SQLExpressDAL.Stock.StockUpdateRow stockRow = null;

        if(table.Rows.Count>0)
        {
            isSuccess = "1";
            xdata = "";
            ydata = "";
            foreach (DataRow dr in table.Rows)
            {
                stockRow = (SQLExpressDAL.Stock.StockUpdateRow)dr;

                xdata +=  String.Format("\"{0:yyyy/MM/dd}\",",stockRow.UpdateTime);
                ydata += String.Format("{0:N2},", stockRow.CMoney);
           }

            xdata = xdata.Remove(xdata.LastIndexOf(","));
            ydata = ydata.Remove(ydata.LastIndexOf(","));

            xdata = String.Format("[{0}]",xdata);
            ydata = String.Format("[{0}]", ydata);
        }

        json = String.Format( json , Request["stockcode"], xdata, ydata, isSuccess);
        json = "[{" + json + "}]";
        return json;
    }
}
